{% extends 'base.html' %}

{% block title %}Manage Category{% endblock %}

{% block content %}
    <div class="page-header">
        <h1>Categories <small class="text-muted">{{ categories | length }}</small></h1>
        <a class="btn btn-secondary" style="float:right;" href="{{ url_for('admin.new_category') }}">New Category</a>
    </div>
    <div class="page-footer">
    {% if categories %}
        <table class="table table-secondary table-striped">
            <thead>
            <tr>
                <th>No.</th>
                <th>Name</th>
                <th>Posts</th>
                <th>Actions</th>
            </tr>
            </thead>
            {% for category in categories %}
                <tr>
                    <td>{{ loop.index }}</td>
                    <td>
                        <a href="{{ url_for('blog.show_category', category_id=category.id) }}">{{ category.name }}</a>
                    </td>
                    <td>
                        {{ category.posts | length }}
                    </td>
                    <td>
                        <a class="btn btn-sm btn-success" {% if category.id == 1 %}style="pointer-events:none"{% endif %}
                           href="{{ url_for('admin.edit_category', category_id=category.id) }}">Edit</a>
                        <form class="inline" method="post"
                              action="{{ url_for('admin.delete_category', category_id=category.id) }}">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <button type="submit" class="btn btn-sm btn-danger" {% if category.id == 1 %}disabled{% endif %}
                                    onclick="return confirm('Are you sure?');">
                                Delete
                            </button>
                        </form>
                    </td>
                </tr>
            {% endfor %}
        </table>
    {% else %}
        <div class="card">
            <div class="card-body tip"><h5>No Posts.</h5></div>
        </div>
    {% endif %}
    </div>
{% endblock %}